package li.brauch.semesterarbeit.radio.views;


import java.util.ArrayList;
import java.util.List;

import li.brauch.semesterarbeit.radio.views.util.ColorStore;
import li.brauch.semesterarbeit.radio.views.util.FontStore;
import li.brauch.semesterarbeit.radio.views.util.ImageStore;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Text;

public class TunerPanel extends Composite {
	
	private List<TunerPanelListener> listeners = new ArrayList<TunerPanelListener>();
	
	private Composite messageComposite;
	private Label icon;
	private Link message;
	private Label title;
	private Combo stationType;
	private Text stationName;
	private Button button;
	
	public TunerPanel(Composite parent) {
		super(parent, SWT.NONE);
		
		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		setLayout(layout);
		setBackground(ColorStore.BACKGROUND);
		GridData layoutdata = new GridData();
		layoutdata.horizontalAlignment = GridData.FILL;
		layoutdata.verticalAlignment = GridData.FILL;
		layoutdata.grabExcessHorizontalSpace = true;
		layoutdata.grabExcessVerticalSpace = true;
		setLayoutData(layoutdata);						
		
		messageComposite = new Composite(this, SWT.NONE);
		{
			messageComposite.setBackground(ColorStore.BACKGROUND);
			messageComposite.setVisible(false);
			layout = new GridLayout();
			layout.numColumns = 2;
			messageComposite.setLayout(layout);
			
			layoutdata = new GridData();
			layoutdata.exclude = true;
			layoutdata.horizontalAlignment = GridData.BEGINNING;
			layoutdata.verticalAlignment = GridData.BEGINNING;
			layoutdata.grabExcessHorizontalSpace = true;
			layoutdata.grabExcessVerticalSpace = false;		
			layoutdata.horizontalSpan = 3;
			messageComposite.setLayoutData(layoutdata);
			
			icon = new Label(messageComposite, SWT.NONE);
			{
				icon.setBackground(ColorStore.BACKGROUND);
				layoutdata = new GridData();
				layoutdata.horizontalAlignment = GridData.BEGINNING;
				layoutdata.verticalAlignment = GridData.BEGINNING;
				layoutdata.grabExcessHorizontalSpace = false;
				layoutdata.grabExcessVerticalSpace = false;			
				icon.setLayoutData(layoutdata);
			}
			
			message = new Link(messageComposite, SWT.NONE);
			{
				message.setBackground(ColorStore.BACKGROUND);
				layoutdata = new GridData();
				layoutdata.horizontalAlignment = GridData.BEGINNING;
				layoutdata.verticalAlignment = GridData.BEGINNING;
				layoutdata.grabExcessHorizontalSpace = true;
				layoutdata.grabExcessVerticalSpace = false;
				message.setLayoutData(layoutdata);
				message.addSelectionListener(new SelectionAdapter() {
					
					public void widgetSelected(SelectionEvent e) {
						notifyLinkClicked(e.text);										
					}
				});
			}
		}
		
		title = new Label(this, SWT.LEFT);
		{
			title.setFont(FontStore.TITLE);
			title.setForeground(ColorStore.FOREGROUND);
			title.setBackground(ColorStore.BACKGROUND);
			layoutdata = new GridData();
			layoutdata.horizontalAlignment = GridData.FILL;
			layoutdata.grabExcessHorizontalSpace = true;
			layoutdata.verticalAlignment = GridData.BEGINNING;
			layoutdata.grabExcessVerticalSpace = false;	
			layoutdata.horizontalSpan = 3;
			title.setLayoutData(layoutdata);
		}
		
		stationType = new Combo(this, SWT.READ_ONLY);
		{
			stationType.setFont(FontStore.TEXT);
			stationType.setBackground(ColorStore.BACKGROUND);
			layoutdata = new GridData();
			layoutdata.horizontalAlignment = GridData.BEGINNING;
			layoutdata.verticalAlignment = GridData.BEGINNING;
			layoutdata.grabExcessHorizontalSpace = false;
			layoutdata.grabExcessVerticalSpace = false;
			stationType.setLayoutData(layoutdata);
			stationType.addSelectionListener(new SelectionAdapter() {				
				public void widgetSelected(SelectionEvent e) {
					doEnabling();
				}
			});
			
		}
		
		stationName = new Text(this, SWT.SINGLE | SWT.BORDER);
		{
			stationName.setFont(FontStore.TEXT);
			stationName.setForeground(ColorStore.FOREGROUND);
			stationName.setBackground(ColorStore.BACKGROUND);
			layoutdata = new GridData();
			layoutdata.horizontalAlignment = GridData.BEGINNING;
			layoutdata.verticalAlignment = GridData.BEGINNING;
			layoutdata.minimumWidth = 200;
			layoutdata.widthHint = 200;
			layoutdata.grabExcessHorizontalSpace = false;
			layoutdata.grabExcessVerticalSpace = false;
			stationName.setLayoutData(layoutdata);
			stationName.addModifyListener(new ModifyListener() {
				public void modifyText(ModifyEvent e) {
					doEnabling();
				}
			});
		}
		
		button = new Button(this, SWT.PUSH | SWT.CENTER);
		{
			button.setBackground(ColorStore.BACKGROUND);
			layoutdata = new GridData();
			layoutdata.horizontalAlignment = GridData.BEGINNING;
			layoutdata.verticalAlignment = GridData.BEGINNING;
			layoutdata.grabExcessHorizontalSpace = false;
			layoutdata.grabExcessVerticalSpace = false;			
			button.setLayoutData(layoutdata);
			button.setImage(ImageStore.CHECK);
			button.setEnabled(false);
			button.addSelectionListener(new SelectionAdapter() {				
				public void widgetSelected(SelectionEvent e) {					
					notifySearchClicked(stationType.getItem(stationType.getSelectionIndex()), stationName.getText());				
				}
			});
		}
	}
	
	public void setStationTypes(String[] types) {
		stationType.setItems(types);
		if (types.length == 1) {
			stationType.select(0);
		}
	}
	
	public void setTitle(String title) {
		this.title.setText(title);
	}
	
	public void showMessage(Message type, String text) {
		
		message.setText(text);
		
		if (type.equals(Message.WARNING)) {
			icon.setImage(ImageStore.WARNING);
		} else {
			icon.setImage(ImageStore.ERROR);
		}
		
		GridData layoutdata = (GridData) messageComposite.getLayoutData();
		layoutdata.exclude = false;
		messageComposite.setVisible(true);
		
		layout();
	}
	
	public void setEnabled(boolean enabled) {
		stationName.setEnabled(enabled);		
		stationType.setEnabled(enabled);
	}
	
	public void clearInput() {
		stationName.setText("");
		stationType.select(-1);
	}
	
	public void hideMessage() {
		GridData layoutdata = (GridData) messageComposite.getLayoutData();
		layoutdata.exclude = true;
		messageComposite.setVisible(false);
		
		layout();
	}
	
	private void doEnabling() {
		button.setEnabled(stationType.getSelectionIndex() > -1 && stationName.getText().length() > 0);		
	}
	
	public void addAdjustPanelListener(TunerPanelListener listener) {
		if (!listeners.contains(listener))
			listeners.add(listener);
	}
	
	public void removeAdjustPanelListener(TunerPanelListener listener) {
		if (listeners.contains(listener))
			listeners.remove(listener);
	}
	
	private void notifyLinkClicked(final String link) {
		
		for (TunerPanelListener listener : listeners) {
			listener.linkClicked(link);
		}
	}
	
	private void notifySearchClicked(final String stationType, final String stationName) {
		
		for (TunerPanelListener listener : listeners) {
			listener.adjustClicked(stationType, stationName);
		}
	}
	
	
}
